package com.gzc.just.play.last.war.common.serializer;

import java.util.function.Function;

/**
 * Protobuf消息反序列化器
 * 提供通用的Protobuf消息反序列化功能
 * @param <T> 消息类型
 */
public class ProtobufDeserializer<T> implements MessageDeserializer<T> {
    
    private final Class<T> messageClass;
    private final int messageId;
    private final Function<byte[], T> deserializeFunction;
    
    /**
     * 构造函数
     * @param messageClass 消息类型
     * @param messageId 消息ID
     * @param deserializeFunction 反序列化函数
     */
    public ProtobufDeserializer(Class<T> messageClass, int messageId, Function<byte[], T> deserializeFunction) {
        this.messageClass = messageClass;
        this.messageId = messageId;
        this.deserializeFunction = deserializeFunction;
    }
    
    @Override
    public T deserialize(byte[] bytes) {
        if (deserializeFunction != null) {
            return deserializeFunction.apply(bytes);
        }
        return null;
    }
    
    /**
     * 获取消息类型
     * @return 消息类型
     */
    public Class<T> getMessageClass() {
        return messageClass;
    }
    
    /**
     * 获取消息ID
     * @return 消息ID
     */
    public int getMessageId() {
        return messageId;
    }
}
